package io.p2p.stream.p2pmain.handler;

import com.alibaba.fastjson.JSONObject;
import common.core.constans.Direction;
import common.core.domain.udp.UdpApiProtocol;
import io.netty.buffer.Unpooled;
import io.netty.channel.socket.DatagramPacket;
import io.p2p.stream.p2pmain.node.ActiveNode;
import io.p2p.stream.p2pmain.session.ActiveNodeSessionManager;
import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;

/**
 * 客户端请求打洞协议类
 */
@Slf4j
public class ClientMakeHoleHandler {

    private UdpApiProtocol apiProtocol;

    public ClientMakeHoleHandler(UdpApiProtocol apiProtocol) {
        this.apiProtocol = apiProtocol;
    }
    public void makeHole() {
        int direction = apiProtocol.getBuffer().getIntValue("direction");
        String from = apiProtocol.getBuffer().getString("from");
        String to = apiProtocol.getBuffer().getString("to");

        if(Direction.Other == direction){
            ActiveNode activeNode = ActiveNodeSessionManager.getInstance().find(to);

            JSONObject p2pMakeHole = new JSONObject();
            p2pMakeHole.put("type","p2pMakeHole");
            p2pMakeHole.put("from",from);
            p2pMakeHole.put("direction", direction);
            p2pMakeHole.put("to",to);
            p2pMakeHole.put("remark","请求打洞");

            apiProtocol.getCtx().writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer(
                    p2pMakeHole.toJSONString(), StandardCharsets.UTF_8), new InetSocketAddress(activeNode.getIp(),activeNode.getPort())));
        } else if(Direction.Local == direction){
            ActiveNode activeNode = ActiveNodeSessionManager.getInstance().find(from);

            JSONObject p2pMakeHole = new JSONObject();
            p2pMakeHole.put("type","p2pMakeHole");
            p2pMakeHole.put("from",from);
            p2pMakeHole.put("direction", direction);
            p2pMakeHole.put("to",to);
            p2pMakeHole.put("remark","请求打洞");

            apiProtocol.getCtx().writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer(
                    p2pMakeHole.toJSONString(), StandardCharsets.UTF_8), new InetSocketAddress(activeNode.getIp(),activeNode.getPort())));
        } else if (Direction.FirstLocalThenOther == direction) {
            ActiveNode activeNode = ActiveNodeSessionManager.getInstance().find(to);

            JSONObject p2pMakeHole = new JSONObject();
            p2pMakeHole.put("type","p2pMakeHole");
            p2pMakeHole.put("from",from);
            p2pMakeHole.put("direction", direction);
            p2pMakeHole.put("to",to);
            p2pMakeHole.put("remark","请求打洞");

            apiProtocol.getCtx().writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer(
                    p2pMakeHole.toJSONString(), StandardCharsets.UTF_8), new InetSocketAddress(activeNode.getIp(),activeNode.getPort())));
        } else if (Direction.FirstOtherThenLocal == direction) {
            ActiveNode toActiveNode = ActiveNodeSessionManager.getInstance().find(to);

            JSONObject p2pMakeHole = new JSONObject();
            p2pMakeHole.put("type","p2pMakeHole");
            p2pMakeHole.put("from",from);
            p2pMakeHole.put("direction", direction);
            p2pMakeHole.put("to",to);
            p2pMakeHole.put("remark","请求打洞");

            apiProtocol.getCtx().writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer(
                    p2pMakeHole.toJSONString(), StandardCharsets.UTF_8), new InetSocketAddress(toActiveNode.getIp(),toActiveNode.getPort())));
        }
    }
}
